﻿// <com.woyouqiu.Copyright>
// --------------------------------------------------------------
// <copyright>上海有求网络科技有限公司 2015</copyright>
// <Solution>MyCompany.MyProject</Solution>
// <Project>MyCompany.MyProject.Application</Project>
// <FileName>CityAppService.cs</FileName>
// <CreateTime>2017-04-22 16:47</CreateTime>
// <Author>何苗</Author>
// <Email>hemiao@woyouqiu.com</Email>
// <log date="2017-04-22 16:47" version="00001">创建</log>
// --------------------------------------------------------------
// </com.woyouqiu.Copyright>

using System.Collections.Generic;
using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Vecen.BangChuang.BangChuang.Areas.DomainDtos;
using Vecen.BangChuang.BangChuang.Areas.Dtos;

namespace Vecen.BangChuang.BangChuang.Areas {
    /// <summary>
    /// 城市操作
    /// </summary>
    public class CityAppService: BangChuangAppServiceBase, ICityAppService {
        private readonly IRepository<City, long> _cityRepository;
        private readonly AddressHelper _addressHelpService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cityRepository"></param>
        /// <param name="ipHelpService"></param>
        public CityAppService(IRepository<City, long> cityRepository, AddressHelper ipHelpService) {
            _cityRepository = cityRepository;
            _addressHelpService = ipHelpService;
        }

        /// <summary>
        /// 根据IP获得城市信息
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <returns></returns>
        [RemoteService(false)]
        public async Task<FullCityOutDto> GetByIp(string ipAddress) {
            var areaCode = await _addressHelpService.GetAreaCodeByIp(ip: ipAddress);
            return GetAddressByAreaCode(areaCode);
        }

        private FullCityOutDto GetAddressByAreaCode(string areaCode) {
            FullCityOutDto result = new FullCityOutDto();
            if(!areaCode.IsNullOrWhiteSpace() && areaCode.Trim().Length >= 6) {
                var provinceId = areaCode.Substring(0, 2).To<int>();
                var cityId = areaCode.Substring(0, 4).To<int>();
                var districtId = areaCode.Substring(0, 6).To<int>();

                result.ProvinceId = provinceId;
                result.ProvinceName = _cityRepository.FirstOrDefault(provinceId)?.Name;
                result.CityId = cityId;
                result.CityName = _cityRepository.FirstOrDefault(cityId)?.Name;
                result.DistrictId = districtId;
                result.DistrictName = _cityRepository.FirstOrDefault(districtId)?.Name;
            }
            return result;
        }

        /// <summary>
        /// 根据坐标获取城市信息
        /// </summary>
        /// <param name="coordinate"></param>
        /// <returns></returns>
        [RemoteService(false)]
        public async Task<FullCityOutDto> GetByCoordinate(Coordinate coordinate) {
            var areaCode = await _addressHelpService.GetAreaCodeByCoordinate(coordinate);

            return GetAddressByAreaCode(areaCode);
        }

        /// <summary>
        /// 根据父节点ID获得子节点
        /// </summary>
        /// <param name="parentId">
        /// 父节点ID,获取省份是为空,
        /// 获取城市时,对应省份ID
        /// 获取区县时,对应城市ID
        /// </param>
        /// <returns></returns>

        public async Task<ListResultDto<CityOutDto>> GetByParentId(int? parentId) {
            var entities = await _cityRepository.GetAllListAsync(c => c.ParentId == parentId);
            return new ListResultDto<CityOutDto>(entities.MapTo<List<CityOutDto>>());
        }

        /// <summary>
        /// 刷新城市数据
        /// </summary>
        /// <returns></returns>
        public Task<int> RefreshCitiesData() {
            throw new System.NotImplementedException();
        }

        /// <summary>
        ///根据ID获得城市信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<CityOutDto> GetById(long id) {
            var city = await _cityRepository.FirstOrDefaultAsync(id);
            return city.MapTo<CityOutDto>();
        }
    }
}